﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Drawing;
using System.IO;
using System.Web.Security;


namespace WebChatClient.Controllers
{
    public class RegisterController : Controller
    {


        HttpClient client = new HttpClient();

        //
        // GET: /Register/

        public RegisterController()
        {
            client.BaseAddress = new Uri("http://localhost:63046/");
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Chatter chatter, HttpPostedFileWrapper imageFile)
        {
            if (ModelState.IsValid)
            {
                Uri ChatterUri = null;



                //Resize image

                byte[] ResizedImage = ResizeImage(imageFile,100,150);



                //Add avatar byte array to Chatter's property
                chatter.Avatar = ResizedImage;

                HttpResponseMessage response = client.PostAsJsonAsync("api/ChatterAPI", chatter).Result;


                if (response.IsSuccessStatusCode)
                {
                    ChatterUri = response.Headers.Location;
                    FormsAuthentication.SetAuthCookie(chatter.Nick, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    //MessageBox.Show((int)response.StatusCode + " - " + response.ReasonPhrase);
                    Response.Write(@"<script language='javascript'>alert('" + (int)response.StatusCode + " - " + response.ReasonPhrase + "')</script>");
                }
            }
            return View();
        }

        private static byte[] ResizeImage(HttpPostedFileWrapper imageFile, int maxHeight, int maxWidth)
        {
            System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(imageFile.InputStream);
            int imageHeight = imageToBeResized.Height;
            int imageWidth = imageToBeResized.Width;

            imageHeight = (imageHeight * maxWidth) / imageWidth;
            imageWidth = maxWidth;

            if (imageHeight > maxHeight)
            {
                imageWidth = (imageWidth * maxHeight) / imageHeight;
                imageHeight = maxHeight;
            }

            Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
            System.IO.MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Position = 0;
            byte[] image = new byte[stream.Length + 1];
            stream.Read(image, 0, image.Length);
            return image;
        }

    }
}
